package paytreasure.service.impl;


import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import paytreasure.dao.IntegralDao;
import paytreasure.po.Integral;
import paytreasure.service.IntegralService;
import paytreasure.util.ConstantUtil;
import paytreasure.vo.ResultVo;

/**
 * 积分
 * @author xhh
 * @date 2017年7月13日
 *
 */

public class IntegralServiceImpl implements IntegralService{
	
	@Autowired
	private IntegralDao integralDao;
	
	private ResultVo result;
    /**
     * 插入新的积分记录
     */
	public ResultVo insertIntegral(Integral inte) {
		
		if(inte.getCreateDate()==null){
			return new ResultVo(ConstantUtil.RESULT_ERROR, "添加失败！");
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
		String monthDate = sdf.format(inte.getCreateDate());
		int count = integralDao.getCount(monthDate);
		if(count > 0) {
			//查询已添加月份
			List<String> month = integralDao.queryAllMonth();
			String string = "";
			for (String str : month) {
				string += str + ",";
			}
			string = string.substring(0,string.length()-1);
			result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "当前已添加",string);
			return result;
		}
		
		int d = integralDao.addIntegral(inte);
		if(d == 0){
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "添加失败！");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "保存成功！");
		return result;
	}
	

	/**
	 * 
	 * 根据相应的ID查询积分记录
	 */
	public ResultVo getIntegralById(String id) {
		
		Integral inte = integralDao.getIntegralById(id);
		if(inte == null) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "未获取到数据");
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "查询成功！",inte);
		return result;
	}

	/**
	 * 
	 * 根据对应的id修改积分信息
	 */
	public ResultVo updateIntegral(Integral inte) {
		
		int d = integralDao.updateIntegral(inte);
		ResultVo result = new ResultVo();
	    if(d > 0){  
	    	result.isResultSuccess();
	    	result.setResultMsg("修改成功");
	    }else{ 
	    	result.isResultError();
	    	result.setResultMsg("修改失败");
	    }  
	    return result;
	}


}
